﻿package fullHistory 
{
	import flash.display.MovieClip;
	/**
	 * ...
	 * @author Nguyen Thi Bich Phuong (Daisy)
	 */
	public class FutureRoutePanel extends MovieClip	{
		public var banker_future_route_item_1:MovieClip;
		public var banker_future_route_item_2:MovieClip;
		public var banker_future_route_item_3:MovieClip;
		public var player_future_route_item_1:MovieClip;
		public var player_future_route_item_2:MovieClip;
		public var player_future_route_item_3:MovieClip;
		
		private static const RESULT:String = "0";
		
		public function FutureRoutePanel() {
			//setDisplayRoute(0, 0, 0, 0, 0, 0);
		}		
		
		
		public function setDisplayFutureRoute(sResultCode:String, type:String):void {		
			
			if (sResultCode.length == 0)
				return;			
			trace("co ve dc hem do");
			var MAX_NUM_CHECKERS:Number = 222;
			var LEN_CODE:int = 1;
			var CODE_COUNT:int = 2;			
			
			var numResultCode:int = int(sResultCode.substr(0, 3));			
			var tmpCode:String = sResultCode.substr(3, numResultCode * 3);
			var offset:int = 3;			
			var lastNum:int = findLastResult( numResultCode, tmpCode ) + 3;						
			var currentCode:String;
			var lastCode:Boolean;			
			var testCode:String = " ";					
			//Type 1
			for( var i:int = 0; i < numResultCode && i < MAX_NUM_CHECKERS; i++ )
			{
				currentCode = sResultCode.substr(offset, LEN_CODE);				
				if( offset == lastNum ) {
					lastCode = true;
					
					if (type == "1") {
						trace("co ve dc hem do:" + currentCode);
						this.setDisplayItem(this.banker_future_route_item_1, Number(currentCode));
					} else if (type == "2") {
						trace("co ve dc hem do22222222:" + currentCode);
						this.setDisplayItem(this.player_future_route_item_1, Number(currentCode));
					}					
				}	
				offset += LEN_CODE + CODE_COUNT;
			}			
			
			
			//Type 2				
			numResultCode = int(sResultCode.substr(offset, 3));
			offset += 3;
			tmpCode = sResultCode.substr(offset, numResultCode * 3);
			lastNum = findLastResult( numResultCode, tmpCode ) + offset;
			for(i = 0; i < numResultCode && i < MAX_NUM_CHECKERS; i++ )
			{
				currentCode = sResultCode.substr(offset, LEN_CODE);
				if ( offset == lastNum ) {
					lastCode = true;
					if (type == "1") {
						this.setDisplayItem(this.banker_future_route_item_2, Number(currentCode));
					} else if (type == "2") {
						this.setDisplayItem(this.player_future_route_item_2, Number(currentCode));
					}					
				}		
				offset += LEN_CODE + CODE_COUNT;
			}
			
			//Type 3			
			numResultCode = parseInt(sResultCode.substr(offset, 3), 10);
			offset += 3;
			tmpCode = sResultCode.substr(offset, numResultCode * 3);
			lastNum = findLastResult( numResultCode, tmpCode ) + offset;
			for(i = 0; i < numResultCode && i < MAX_NUM_CHECKERS; i++ )
			{
				currentCode = sResultCode.substr(offset, LEN_CODE);
				if ( offset == lastNum ) {
					lastCode = true;
					if (type == "1") {
						this.setDisplayItem(this.banker_future_route_item_3, Number(currentCode));
					} else if (type == "2") {
						this.setDisplayItem(this.player_future_route_item_3, Number(currentCode));
					}					
				}		
				offset += LEN_CODE + CODE_COUNT;
			}	
		}
		
		private function findLastResult(numResultCode:int, sResultCode:String):int {
			var NumCol:int = int(numResultCode/6);
			var lastNum:int = 0;
			var lastNumCode:String = FutureRoutePanel.RESULT;
			// find last column
			for (var i:int = NumCol; i>0; i--) {
				lastNum = (i-1)*18;
				lastNumCode = sResultCode.substr(lastNum, 1);
				if (lastNumCode != FutureRoutePanel.RESULT) {
					break;
				}
			}
			// parse last column
			for (var j:int = 1; j<=5; j++) {
				var tmp:int = lastNum+3;
				var tmpCode:String = sResultCode.substr(tmp, 1);
				if (tmpCode == lastNumCode) {
					lastNum = tmp;
					// test if tail exists
					if ((tmp+18)<numResultCode*3) {
						if (sResultCode.substr(tmp+18, 1) == lastNumCode) {
							break;
						}
					}
					if ((tmp-18)>0) {
						if (sResultCode.substr(tmp-18, 1) == lastNumCode) {
							break;
						}
					}
				} else {
					break;
				}
			} // if tail goes sideway
			var tmpNum:int = lastNum;
			var findTail:Boolean = false;
			while (tmpNum<((numResultCode*3)-3)) {
				tmpNum = tmpNum+18;
				if (tmpNum<((numResultCode*3)-3)) {
					if (sResultCode.substr(tmpNum, 1) != FutureRoutePanel.RESULT) {
						lastNum = tmpNum;
					}
					findTail = true;
				}
			}
			if (!findTail) {
				tmpNum = lastNum;
				while (tmpNum>15) {
					tmpNum = tmpNum-18;
					if (tmpNum>15) {
						if (sResultCode.substr(tmpNum, 1) == lastNumCode) {
							lastNum = tmpNum;
						} else {
							break;
						}
					}
				}
			}
			return lastNum;
		}
		
		private function setDisplayRoute(banker_1:int, banker_2:int, banker_3:int, player_1:int, player_2:int, player_3:int):void {
			setDisplayItem(this.banker_future_route_item_1, banker_1);
			setDisplayItem(this.banker_future_route_item_2, banker_2);
			setDisplayItem(this.banker_future_route_item_3, banker_3);
			setDisplayItem(this.player_future_route_item_1, player_1);
			setDisplayItem(this.player_future_route_item_2, player_2);
			setDisplayItem(this.player_future_route_item_3, player_3);
		}
		
		private function setDisplayItem(route_item:MovieClip, _value:int):void {
			switch(_value) {
				case 1:
					route_item.gotoAndStop("banker");
					break;
				case 2:
					route_item.gotoAndStop("player");
					break;				
				default: 
					route_item.gotoAndStop("none");
					break;
			}
		}
	}

}